package com.tuanzi.loan.business.repository.credithousekeeper.impl;

import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.repository.credithousekeeper.CreditCompensatoryOrderRepository;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.credithousekeeper.CreditCompensatoryOrderRequest;
import com.tuanzi.loan.vo.response.credithousekeeper.CreditCompensatoryOrderResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.*;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCardCompensationOrder.LOAN_CARD_COMPENSATION_ORDER;

/**
* @param:
* @Description:    信用卡代偿订单查询sql
* @Author:         xionghao
* @CreateDate:      2018-07-18
*/
@SuppressWarnings({ "rawtypes" })
@Repository
public class CreditCompensatoryOrderRepositoryImpl extends JOOQBaseRepository implements CreditCompensatoryOrderRepository{

    @Autowired
    private DictionaryService dictionaryService;
    @Override
    protected TableImpl getTable() {
        return LOAN_CARD_COMPENSATION_ORDER;
    }

    private static Map<Integer,String> ORDER_SOURCE_MAP = new HashMap<>();

    /**
     * 通过合作方APItype 获取数据字典甲方
     * @param type
     * @return
     */
    private String orderSourceByType(Integer type){
        if (Objects.nonNull(type)){
            return dictionaryService.findValueByCodeAndName(DictionaryCode.DAICHANGTYPE,String.valueOf(type));
        }else{
            return null;
        }
    }

    @Override
    public Page<CreditCompensatoryOrderResponse> list(CreditCompensatoryOrderRequest searchRequest, Pageable pageable) {
        List<Integer> list;
        //只选取 待确认，处理中，成功，失败，中止，异常的 状态订单
        list = Arrays.asList(new Integer[]{10,11,12,13,14,15});
        fields(LOAN_CARD_COMPENSATION_ORDER.fields());
        eq(LOAN_CARD_COMPENSATION_ORDER.ID,searchRequest.getId());
        eq(LOAN_CARD_COMPENSATION_ORDER.TRANS_ID,searchRequest.getTrans_id());
        eq(LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM,searchRequest.getChannel_from());
        eq(LOAN_CARD_COMPENSATION_ORDER.PRODUCT_ID,searchRequest.getProduct_id());
        eq(LOAN_CARD_COMPENSATION_ORDER.PETNAME,searchRequest.getPetname());
        eq(LOAN_CARD_COMPENSATION_ORDER.MOBNUM,searchRequest.getMobnum());
        eq(LOAN_CARD_COMPENSATION_ORDER.IDCARD,searchRequest.getIdcard());
        eq(LOAN_CARD_COMPENSATION_ORDER.STATUS,searchRequest.getStatus());
        eq(LOAN_CARD_COMPENSATION_ORDER.TYPE,searchRequest.getSource());
        in(LOAN_CARD_COMPENSATION_ORDER.STATUS,list);
        between(LOAN_CARD_COMPENSATION_ORDER.CTIME, searchRequest.getStartDate(),
                searchRequest.getEndDate() == null ? searchRequest.getEndDate() :
                        DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()) + " 23:59:59");
        orderBy(LOAN_CARD_COMPENSATION_ORDER.CTIME, Sort.Direction.DESC);
        return search(pageable,record -> {
            CreditCompensatoryOrderResponse response = new CreditCompensatoryOrderResponse();
            response.setCreatetime(record.get(LOAN_CARD_COMPENSATION_ORDER.CTIME));
            response.setOrderid( record.get( LOAN_CARD_COMPENSATION_ORDER.ID));
            response.setTrans_id(record.get(LOAN_CARD_COMPENSATION_ORDER.TRANS_ID));
            response.setPrdocu_id(record.get(LOAN_CARD_COMPENSATION_ORDER.PRODUCT_ID));
            response.setChannel_from(record.get(LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM));
            response.setPetname(PhoneNumNideUtil.hideName(record.get(LOAN_CARD_COMPENSATION_ORDER.PETNAME)));
            response.setMobnum(PhoneNumNideUtil.hideNum(record.get(LOAN_CARD_COMPENSATION_ORDER.MOBNUM)));
            response.setIdcard(PhoneNumNideUtil.hideIdCard(record.get(LOAN_CARD_COMPENSATION_ORDER.IDCARD)));
            response.setBankname(record.get(LOAN_CARD_COMPENSATION_ORDER.BANK_NAME));
            response.setCard_no(PhoneNumNideUtil.hideBankCard(record.get(LOAN_CARD_COMPENSATION_ORDER.CARD_NO)));
            response.setAmount(record.get(LOAN_CARD_COMPENSATION_ORDER.AMOUNT));
            response.setPeroid(record.get(LOAN_CARD_COMPENSATION_ORDER.PEROID));
            response.setFee(record.get(LOAN_CARD_COMPENSATION_ORDER.FEE));
            response.setBill_date(record.get(LOAN_CARD_COMPENSATION_ORDER.BILL_DATE));
            response.setStatus(record.get(LOAN_CARD_COMPENSATION_ORDER.STATUS));
            response.setChannel(record.get(LOAN_CARD_COMPENSATION_ORDER.CHANNEL));
            response.setPlatform(record.get(LOAN_CARD_COMPENSATION_ORDER.PLATFORM));
            response.setLeft_limit(record.get(LOAN_CARD_COMPENSATION_ORDER.LEFT_LIMIT));
            response.setSource(orderSourceByType(record.get(LOAN_CARD_COMPENSATION_ORDER.TYPE)));
            return response;
        });
    }
}
